package java学习.第16章_网络编程;

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class E_TCP {
    @Test //客户端
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        InputStream is = null;
        //字节数组流，将字节流合成一起后在输出，不出现乱码
        ByteArrayOutputStream baos = null;
        try {
            //创建Socket对象，指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getLocalHost();
            socket = new Socket(inet,65534);//接受方的端口号地址
            os = socket.getOutputStream();//获取输出流  获得输出数据
            os.write("你好，我是客户端\n".getBytes());//写出数据操作
            os.flush();
            socket.shutdownOutput();//关闭输出流 让服务端直到我发送结束了

            baos = new ByteArrayOutputStream();
            //获取接收的数据 输入流
            is = socket.getInputStream();
            byte[] bytes = new byte[5];
            int len;
            while ((len = is.read(bytes)) != -1) {
                //把输入流的内容  写道ByteArrayOutputStream里
                baos.write(bytes, 0, len);
            }
            System.out.println(socket.getInetAddress() + ":" + baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
                baos.close();
                is.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    @Test //服务端
    public void server(){
        ServerSocket ss = null;
        Socket socket = null;

        InputStream ips = null;
        OutputStream ops = null;
        //字节数组流，将字节流合成一起后在输出，不出现乱码
        ByteArrayOutputStream baos = null;
        try {
            //创建服务器ServerSocket对象，指明自己和端口号
            ss = new ServerSocket(65534);

            while (true) {
                socket = ss.accept();//接受客户端的Socket
                System.out.println(socket.getInetAddress().getHostAddress()+"连接了服务器");
                ips = socket.getInputStream();//获取字节输入流
                baos = new ByteArrayOutputStream();//防止汉字乱码
                byte[] bytes = new byte[5];
                int len;
                while ((len = ips.read(bytes)) != -1) {
                    //把输入流的内容  写道ByteArrayOutputStream里
                    baos.write(bytes, 0, len);
                }
                System.out.println(socket.getInetAddress() + ":" + baos.toString());

                ops = socket.getOutputStream();//获取输出流，给客户端返回信息
                ops.write("我收到了 你好".getBytes());
                socket.shutdownOutput();//不刷新，对方认为消息没发送完毕

            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ops.close();
                baos.close();
                ips.close();
                socket.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
